// pages/opt_user_admin/opt_page/cur_piple_elec/piples_elec/piples_elec.js
var app = getApp()
//常量数据配置
var ConstantData = require('../../../../../../common/constant_data.js')
var uitl = require('../../../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pipleInfoList: [],
    pipleCount:0,
    noramlCheck:true,
    questionCheck:true,
    chairOnPipleCheck:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    }); 
    var that = this;
    var typeId = options.typeId;
    var hospitalName = options.hospitalName;
    that.setData({
      hospitalName: hospitalName
    })

    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/low_electricity/piples',
        data: {
          tid: typeId,
          lowElec:1000000
        },
        header: header,
        success: function (res) {
          getApp().hideLoading();
          console.log("chair nums---->", res);
          if (res.data.result === 0) {
            var questionPipleList = new Array();
            var normalPipleList = new Array();
            var chairOnPipleList = new Array();
            var pipleList = res.data.data;
            var overTimeLen = 3 * 24 * 60 * 60 * 1000;
            for (let i = 0; i < pipleList.length; i++) {
              if (!pipleList[i].created || !pipleList[i].hpe_electricity) {
                pipleList[i].isMark = true;
                questionPipleList.push(pipleList[i]);
                continue;
              }
              
              var timestamp = new Date().getTime();
              var diff = timestamp - pipleList[i].created;
              var minElec = pipleList[i].min_electricity;
              if (diff >= overTimeLen || pipleList[i].hpe_electricity < minElec || pipleList[i].hp_stat != 1 || pipleList[i].oneMinuteInfo) {
                pipleList[i].isMark = true;
                pipleList[i].diff = diff;
                pipleList[i].overTimeLen = overTimeLen;
                questionPipleList.push(pipleList[i]);
              }else {
                pipleList[i].isMark = false;
                normalPipleList.push(pipleList[i]);
              }
              pipleList[i].created = uitl.timestampToTime(pipleList[i].created);
              
              //桩上轮椅
              if (pipleList[i].onPipleChairInfo) {
                pipleList[i].onPipleChairInfo.uur_receive_return_time = uitl.timestampToTime(pipleList[i].onPipleChairInfo.uur_receive_return_time);
                chairOnPipleList.push(pipleList[i]);
              }
              

            }
            that.setData({
              pipleInfoList: res.data.data,
              pipleInfoOriginList: res.data.data,
              inputSearchPipleInfoList: res.data.data,
              questionPipleList: questionPipleList,
              normalPipleList: normalPipleList,
              chairOnPipleList: chairOnPipleList,
              homingStatInfo: res.data.pipleStatInfo,
              pipleCount: pipleList.length,
              normalCount: normalPipleList.length,
              questionCount: questionPipleList.length,
              chairOnPipleCount: chairOnPipleList.length, 
              noramlCheck: true,
              questionCheck: true,
              chairOnPipleCheck: false
            })
          }
        }
      })
    });  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 搜索
   */ 
  inputKey:function(e) {

    this.setData({
      inputKey: e.detail.value,
      noramlCheck: true,
      questionCheck: true,
      chairOnPipleCheck: false
    });

    var piples = new Array();
    var pipleInfoOriginList = this.data.pipleInfoOriginList;
    for (var i = 0; i < pipleInfoOriginList.length; i++) {
      if (pipleInfoOriginList[i].hpe_id == this.data.inputKey || pipleInfoOriginList[i].hp_position.indexOf(this.data.inputKey) != -1) {
        piples.push(pipleInfoOriginList[i]);
      }
    }
    if (this.data.inputKey == '') {
      piples = this.data.pipleInfoOriginList;
    } else if (piples.length == 0) {
      this.setData({
        noneDataStat: true
      });
    } else {
      this.setData({
        noneDataStat: false
      });
    }
    this.setData({
      pipleInfoList: piples,
      pipleCount:piples.length
    })

  },

  /**
   * 按类别查看桩电量信息
   */  
  pipleSearch:function(e) {
    var vals = e.detail.value;
    var v = 0;
    for (var i = 0; i < vals.length; i++) {    
      v += parseInt(vals[i]);
    }    

    if (vals.length == 1 && parseInt(vals[0]) == 3) {
      v = 4;
    }else {
      this.setData({
        chairOnPipleCheck: false
      })
    }


    switch (v) {
      case 0:
        this.setData({
          pipleInfoList: [],
          noramlCheck: false,
          questionCheck: false
        })
        break;
      case 1:
        this.setData({
          pipleInfoList: this.data.normalPipleList,
          noramlCheck: true,
          questionCheck: false
        })
        break;
      case 2:
        this.setData({
          pipleInfoList: this.data.questionPipleList,
          noramlCheck: false,
          questionCheck: true
        })
        break;
      case 3:
        this.setData({
          pipleInfoList: this.data.pipleInfoOriginList,
          noramlCheck: true,
          questionCheck: true
        });
        break;
      case 4:
        this.setData({
          pipleInfoList: this.data.chairOnPipleList,
          noramlCheck: false,
          questionCheck: false,
          chairOnPipleCheck: true
        });
        break;  
    }
    this.setData({
      inputSearchPipleInfoList: this.data.pipleInfoList,
      pipleCount: this.data.pipleInfoList.length
    })
    
  }




})